<?php
/***************************************
 *
 * @Repositories 模型层~数据表配置及自动验证等~~
 *
 **************************************/
namespace app\common\model;

use think\Model;
use think\Validate;

class CalendarModel extends Model
{
    protected $autoWriteTimestamp = false;
    protected $updateTime = false;
    protected $createTime = false;


    /*
     * var string 数据表名
    */
    protected $table = 'calendar';

    //自动验证
    protected $rule =   [
        'user_id'  => 'require|number',
        'mood'  => 'require',
        'img' => 'require',
        'create_time' => 'require'
    ];

    //验证返回结果
    protected $message  =   [
        'user_id.require' => 'user_id必须填写！',
        'user_id.number'   => 'user_id必须是数字！',
        'mood.require' => '每日心情必须填写！',
        'img.require' => '图片参数必须填写',
        'create_time.require' => '时间戳必填'
    ];

    /*添加数据时候验证字段是否匹配*/
    public function  checkData($data){
        $validate = new Validate($this->rule,$this->message);
        $data = $validate->check($data);
        if($data == false){
            $rsl_yz = $validate->getError();
            return  $rsl_yz;
        }else{
            return 'success';
        }
    }

    /*删除数组里面数据库没有的字段*/
    public function unset_data($data, $field){
        foreach ($data as $key => $val) {
            if (!in_array($key, $field)) {
                unset($data[$key]);
            }
        }
        return $data;
    }
}